package com.mySampleApplication.server;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.Db;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.handler.EntityHandler;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mySampleApplication.client.dmp.DmpDto;
import com.mySampleApplication.client.dmp.DmpService;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

/**
 * 获取数据中台的信息类
 */
public class DmpServlet extends RemoteServiceServlet implements DmpService {

    /**
     * 获取数据对象
     * @param map 条件数据
     * @param object 对象表
     * @return
     */
    public DmpDto getSelectMod(Map<String, String> map, String object){
        DmpDto dto =new DmpDto();
        Map<String, Object> reMap = new HashMap<>();
        StringBuffer str = new StringBuffer();
        DataSource ds = DSFactory.get("mysql_dmp");
        Db.use(ds);
        Connection conn = null;
        try {
            if(map.size()>0 && !map.isEmpty()){
                map.forEach((k,v)->{
                    if(map.size()==1){
                        str.append(k).append(" like ").append("'%"+v+"%'");
                    }else if (map.size()>1){
                        str.append(" and ").append(k).append("like").append("'%"+v+"%'");
                    }

                });
            }

            conn = ds.getConnection();
            String sql="select * from "+object;
            if(!str.toString().equals("")){
                sql=sql+" where " +str.toString();
            }

            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler());
            reMap.put("entityList", BeanUtil.copyToList(entityList, Map.class));
            List<Map<String,String>> list =new ArrayList<>();
            Map<String, String> mapp;
            for (Map  a:BeanUtil.copyToList(entityList, Map.class)) {
                mapp= new HashMap<>();
                Map<String, String> finalMapp = mapp;
                a.forEach((k, v)->{
                    finalMapp.put(String.valueOf(k),v==null?"":v+"") ;
                });
                //list.add(finalMapp);
                dto.setKeyValueMap(finalMapp);
            }

        } catch (SQLException e) {

        } finally {
            DbUtil.close(conn);
        }
        return dto;
    }

    /**
     * 获取数据对象属性
     * @param map 条件数据
     * @param mapTable 模型表
     * @return
     */
    public  DmpDto getSelectByObjectId(Map<String, String> map,String mapTable){
        DmpDto dto =new DmpDto();
        Map<String, Object> reMap = new HashMap<>();
        StringBuffer str = new StringBuffer();
        DataSource ds = DSFactory.get("mysql_dmp");
        Db.use(ds);
        Connection conn = null;
        try {
            if(map.size()>0 && !map.isEmpty()){
                map.forEach((k,v)->{
                    if(map.size()==1){
                        str.append(k).append("=").append("'"+v+"'");
                    }else if (map.size()>1){
                        str.append(" and ").append(k).append("like").append("'%"+v+"%'");
                    }

                });
            }

            conn = ds.getConnection();
            String sql="select db_field_name,name_cn from "+mapTable;
            if(!str.toString().equals("")){
                sql=sql+" where " +str.toString()+" order by create_date";
            }

            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler());
            reMap.put("entityList",BeanUtil.copyToList(entityList, Map.class));
            List<Map<String,String>> list =new ArrayList<>();
            LinkedHashMap<String, String> mapp= new LinkedHashMap<>();
            for (Map  a:BeanUtil.copyToList(entityList, Map.class)) {

                mapp.put(a.get("db_field_name").toString(),a.get("name_cn").toString()) ;

            }
            dto.setKeyValueMap(mapp);
        } catch (SQLException e) {

        } finally {
            DbUtil.close(conn);
        }
        return dto;
    }

    /**
     * 获取数据对象属性
     * @return
     */
    public  DmpDto selectObjectList(){
        DmpDto dto =new DmpDto();
        Map<String, Object> reMap = new HashMap<>();
        DataSource ds = DSFactory.get("mysql_dmp");
        Db.use(ds);
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql="select id,name_cn,name_en from bt_data_object";
            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler());
            reMap.put("entityList",BeanUtil.copyToList(entityList, Map.class));
            List<Map<String,String>> list =new ArrayList<>();
            Map<String, String> mapp;
            for (Map  a:BeanUtil.copyToList(entityList, Map.class)) {
                mapp= new HashMap<>();
                Map<String, String> finalMapp = mapp;
                a.forEach((k, v)->{
                    finalMapp.put(String.valueOf(k),v==null?"":v+"") ;
                });
                list.add(finalMapp);
            }
            dto.setKeyValueListMap(list);
        } catch (SQLException e) {

        } finally {
            DbUtil.close(conn);
        }
        return dto;
    }


    /**
     * 获取数据对象属性
     * @return
     */
    public  DmpDto selectObjectListBy(Map<String, String> map){
        DmpDto dto =new DmpDto();
        Map<String, Object> reMap = new HashMap<>();
        StringBuffer str = new StringBuffer();
        DataSource ds = DSFactory.get("mysql_dmp");
        Db.use(ds);
        Connection conn = null;
        try {
            if(map.size()>0 && !map.isEmpty()){
                map.forEach((k,v)->{
                    if(k.equals("category_id")){
                        str.append(k).append("=").append("'"+v+"'");
                    }else{
                        str.append(k).append(" like ").append("'%"+v+"%'");
                    }
                });
            }
            conn = ds.getConnection();
            String sql="select id,name_cn,name_en from bt_data_object ";
            if(!str.toString().equals("")){
                sql=sql+" where " +str+" order by create_date";
            }
            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler());
            reMap.put("entityList",BeanUtil.copyToList(entityList, Map.class));
            List<Map<String,String>> list =new ArrayList<>();
            Map<String, String> mapp;
            for (Map  a:BeanUtil.copyToList(entityList, Map.class)) {
                mapp= new HashMap<>();
                Map<String, String> finalMapp = mapp;
                a.forEach((k, v)->{
                    finalMapp.put(String.valueOf(k),v==null?"":v+"") ;
                });
                list.add(finalMapp);
            }
            dto.setKeyValueListMap(list);
        } catch (SQLException e) {
        } finally {
            DbUtil.close(conn);
        }
        return dto;
    }


    /**
     * 获取数据对象分类
     * @return
     */
    public  DmpDto getDataElementCategoryList(){
        DmpDto dto =new DmpDto();
        Map<String, Object> reMap = new HashMap<>();
        DataSource ds = DSFactory.get("mysql_dmp");
        Db.use(ds);
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql="select id,parent_id,name from bt_data_element_category";
            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler());
            reMap.put("entityList",BeanUtil.copyToList(entityList, Map.class));
            List<Map<String,String>> list =new ArrayList<>();
            Map<String, String> mapp;
            for (Map  a:BeanUtil.copyToList(entityList, Map.class)) {
                mapp= new HashMap<>();
                Map<String, String> finalMapp = mapp;
                a.forEach((k, v)->{
                    finalMapp.put(String.valueOf(k),v==null?"":v+"") ;
                });
                list.add(finalMapp);
            }
            dto.setKeyValueListMap(list);
        } catch (SQLException e) {

        } finally {
            DbUtil.close(conn);
        }
        return dto;
    }

    /**
     * 根据数据对象id获取数据对象
     * @param id
     * @param object 对象表
     * @return
     */
    public static   Map<String, String> getSelectModById(String id, String object){
        StringBuffer str = new StringBuffer();
        DataSource ds = DSFactory.get("mysql_dmp");
        Db.use(ds);
        Connection conn = null;
        Map<String, String> mapp= new HashMap<>();
        try {

            conn = ds.getConnection();
            String sql="select * from "+object+" where id="+"'"+id+"'";

            Entity entityList = SqlExecutor.query(conn, sql, new EntityHandler());

            List<Map<String,String>> list =new ArrayList<>();

            BeanUtil.beanToMap(entityList).forEach((k, v)->{
                mapp.put(String.valueOf(k),v==null?"":v+"") ;
                });


        } catch (SQLException e) {

        } finally {
            DbUtil.close(conn);
        }
        return mapp;
    }
    /**
     * 根据数据对象id获取数据对象
     * @param id
     * @return
     */
    public String getObjectName(String id){

        Map<String, String> data=getSelectModById(id,"bt_data_object");
        System.out.println(data.get("name_cn"));
        return data.get("name_cn");
    }



}
